from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
X = 2*np.random.rand(100,1)
y = 4 + 3*X +np.random.rand(100,1)
plt.plot(X,y,'b.')
plt.xlabel('X_1')
plt.ylabel('y')
plt.axis([0,2,0,15])
plt.show()

# 使用sklearn的函数解决线性回归问题
lin_reg = LinearRegression()
lin_reg.fit(X,y)
print(lin_reg.coef_)
print(lin_reg.intercept_)

# 批量梯度下降
X_b = np.append(np.ones((100,1)),X,axis=1)
eta = 0.1
n_iterations = 1000
m = 100
theta = np.random.randn(2,1)
for iteration in range(n_iterations):
    gradients = 2/m*X_b.T.dot(X_b.dot(theta)-y)
    theta = theta-eta*gradients
print(theta)
X_new = np.array([[0],[2]])
X_new_b = np.append(np.ones((2,1)),X_new,axis=1)
y_predict=X_new_b.dot(theta)
plt.plot(X,y,'b.')
plt.xlabel('X_1')
plt.ylabel('y')
plt.plot(X_new,y_predict,'r-')
plt.axis([0,2,0,15])
plt.show()


